import Vue from 'vue'
import Vuex from 'vuex'
// 导入持久化存储模块
import persisted from 'vuex-persistedstate'
// 导入axios
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [persisted()],
  state: {
    token: '',
    // 定义用户信息对象
    userInfo: {}
  },
  mutations: {
    updateToken (state, payload) {
      state.token = payload
    },
    updateUserInfo (state, info) {
      state.userInfo = info
    }
  },
  actions: {
    //   定义初始化用户基本信息的 action函数
    async initUserInfo (context) {
      const { data: res } = await axios.get('/my/userinfo')
      if (res.code === 0) {
        context.commit('updateUserInfo', res.data)
      }
    }
  },
  modules: {
  }
})
